package com.emip.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.HourStoreBean;
import com.emip.dao.HourStoreDAO;
import com.emip.util.ReportFormat;


@Repository("hourStoreDAO")
public class HourStoreDAOImpl extends StoreDAO implements HourStoreDAO {

	
	public List<HourStoreBean> getByTimespan(String startDate, String endDate) {
		return this.getByTimespan(startDate, endDate, null, null);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<HourStoreBean> getByTimespan(String startDate, String endDate, String[] items, String siteId) {
		return getHibernateTemplate().find(getHourHQL(startDate, endDate, items, siteId));
	}
	
	public int add(HourStoreBean hourStoreBean) {
		Integer id = (Integer) this.getHibernateTemplate().save(hourStoreBean);
		return id.intValue();
	}

	public HourStoreBean merge(HourStoreBean store) {
		return getHibernateTemplate().merge(store);
	}

	public long getCount(String startDate, String endDate, String[] itemIds, String siteId) {
		String hql = "select count(distinct entity.genTime) " + getHourHQL(startDate, endDate, itemIds, siteId);
		return (Long)getCount(hql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Object[]> getInfo(String startDate, String endDate, String[] itemIds, String siteId) {
		String hql = "select min(entity.genTime) as mindate, max(entity.genTime) " +
				"as maxdate, count(distinct entity.genTime) ";
		hql += getHourHQL(startDate, endDate, itemIds, siteId);
		return getHibernateTemplate().find(hql);
	}

	public HourStoreBean findById(int id) {
		return getHibernateTemplate().get(HourStoreBean.class, id);
	}

	public List<HourStoreBean> getByTimespan(String startDate, String endDate,
			String[] itemIds, String siteId, int page, int size) {
		return getDatePage(HourStoreBean.class, ReportFormat.HOURFORMAT, startDate, endDate, itemIds, siteId, page, size);
	}
	
	@SuppressWarnings("unchecked")
	public List<Date> getSEDate(String startDate, String endDate) {
		String hql = "select max(entity.genTime) as maxdate, min(entity.genTime) " +
				"as mindate " + getHourHQL(startDate, endDate, null, null);
		List<Object[]> list = getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			Object[] objs = list.get(0);
			List<Date> dates = new ArrayList<Date>();
			dates.add((Date) objs[1]);
			dates.add((Date) objs[0]);
			return dates;
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public Date getLastDate(String itemId, String siteId) {
		String hql = "select max(b.genTime) from HourStoreBean b where 1 = 1";
		if (itemId != null) {
			hql += " and b.item.id = '" + itemId + "'";
		}
		if (siteId != null) {
			hql += " and b.site.id = '" + siteId + "'";
		}
		List<Date> list = getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	public List<HourStoreBean> getLastNItemsBySiteID(String siteId,
			String itemId, int num) {
		String hql = "from HourStoreBean h where h.site.id = '" + siteId +
				"' and h.item.id = '" + itemId + "' order by h.genTime desc";
		return this.getEntities(HourStoreBean.class, hql, 1, num);
	}
	
	public Object[] getStatistics(String startDate, String endDate, String itemIds, String siteId) {
		long count = this.getCount(startDate, endDate, new String[]{itemIds}, siteId);
		if (count == 0) return null;
		String[] result = this.getDateStatistic("Hour", "YYYY-MM-DD HH24:Mi:SS", startDate+":00:00", endDate+":59:59", new String[]{itemIds}, siteId);
		result[3] = Integer.parseInt(result[3])*100/count + "%";
		return result;
	}


	public HourStoreBean findByHour(String date, String siteId, String itemId) {
		List<HourStoreBean> hours = this.getByTimespan(date, date, new String[]{itemId}, siteId);
		if (hours != null && hours.size() > 0) {
			return hours.get(0);
		}
		return null;
	}

}
